package com.cheer.vo;

import cn.hutool.core.bean.BeanUtil;
import com.cheer.entity.LecCourseEntity;
import com.cheer.entity.LecTeacherEntity;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Data;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class LecCourseTeaExportVO {

    private Integer rank;

    private Long count;

    private String name;

    private String province;

    private String org;

    private String department;

    private String profession;

    private String direction;

    private String description;

    public static Map<String, String> headerAlias = Maps.newLinkedHashMap();

    static{
        headerAlias.put("rank","排名");
        headerAlias.put("count","课程次数");
        headerAlias.put("name","姓名");
        headerAlias.put("province","省份");
        headerAlias.put("org","机构");
        headerAlias.put("department","单位");
        headerAlias.put("profession","职务");
        headerAlias.put("direction","宣讲方向");
        headerAlias.put("description","简介");
    }

    /**
     * 转换
     * @param idMapLecTeacherEntitys
     * @param list
     * @return
     */
    public static List<LecCourseTeaExportVO> convertTOLecCourseTeaExportVO( Map<Integer, List<LecTeacherEntity>> idMapLecTeacherEntitys,
                                                                            List<LecCourseEntity> list ) {
        List<LecCourseTeaExportVO> convertResult = Lists.newArrayList();
        for (Integer teaId : idMapLecTeacherEntitys.keySet()) {

            List<LecTeacherEntity> lecTeacherEntities = idMapLecTeacherEntitys.get(teaId);
            LecTeacherEntity lecTeacherEntity = lecTeacherEntities.get(0);
            LecCourseTeaExportVO lecCourseTeaExportVO = new LecCourseTeaExportVO();
            BeanUtil.copyProperties(lecTeacherEntity, lecCourseTeaExportVO, true);
            long count = list.stream().filter(lecCourseEntity -> lecCourseEntity.getTeacherId().equals(teaId)).count();
            lecCourseTeaExportVO.setCount(count);
            convertResult.add(lecCourseTeaExportVO);
        }
        Comparator<LecCourseTeaExportVO> comparator = Comparator.comparing(LecCourseTeaExportVO::getCount);
        // 返回比较之后的结果
        List<LecCourseTeaExportVO> collect = convertResult.stream().sorted(comparator).collect(Collectors.toList());
        for (int i = 0; i < collect.size(); i++) {
            LecCourseTeaExportVO lecCourseTeaExportVO = collect.get(i);
            lecCourseTeaExportVO.setRank(i+1);
        }

        return collect;
    }
}
